/**
 * Created by ljg on 2017/4/27.
 */
/**
 *
 * @param id 生成的元素id
 * @param url 地区数据的url
 * */
function initOrganizaiton(id,url) {
    $("#"+id).append(' <h3>组织机构查询</h3>' +
                        '<select name="parentOrganization"> ' +
                            '<option>请选择父级机构</option> ' +
                        '</select> ' +
                        '<select name="organization"> ' +
                            '<option>请选择机构</option> ' +
                        '</select> ' +
                        '<table border="2" class="organization"> ' +
                            '<thead align="center"><tr><th>对应的机构名称</th><th id="parentOrganizaitonName"></th><th id="organizaitonName"></th></tr></thead> ' +
                            '<tbody align="center"><tr><td>对应的机构编码</td><td id="parentOrganizaitonCode"></td><td  id="organizaitonCode"></td></tr></tbody> ' +
                        '</table>');
    function loopOption(datas,val) {
        var str="";
        for (let i in datas){
            var selected = datas[i].code==val?'selected':'';
            str+="<option value='"+datas[i].code+"' "+selected+" class='ng-scope'>"+datas[i].name+"</option>";
        }
        return str;
    }
    function changeValue(v,parentOrganizationData,organizationData) {
        var value = v || '';
        var defaulyData = parentOrganizationData[value] || {};
        var childData = {};
        switch (defaulyData.level+"") {
            case "1":
                childData = organizationData[value].children;
                $("select[name='parentOrganization']").html('').append($("<option>请选择父级组织机构</option>")).append($(loopOption(organizationData,defaulyData.code)));
                $("select[name='organization']").html('').append($("<option>请选择组织机构</option>")).append($(loopOption(childData)));
                break;
            default:
                $("select[name='parentOrganization']").html('').append($("<option>请选择父级组织机构</option>")).append($(loopOption(organizationData)));
                $("select[name='organization']").html('').append($("<option>请选择组织机构</option>"));
                break;
        }
    }
    var parentOrganizationData={};
    var organizationData={};
    $.ajax({
        url:url,
        type:'get',
        success:function (result) {
            var dataList = result.data||[];
            var childOrganizationData = {};
            var level = 0;
            for(var i in dataList){
                var data = dataList[i]||{};
                if( data.parentCode == null || data.parentCode == "" ){
                    level = 1;
                }else{
                    level = 2;
                }
                var dd ={parentCode:data.parentCode,name:data.name,code:data.orgCode,level:level};
                parentOrganizationData[data.orgCode] = dd;
                if( level == 1 ){
                    organizationData[data.orgCode] = dd;
                }else{
                    childOrganizationData[data.orgCode] = dd;
                }
            }

            for (var i in childOrganizationData){
                var cData=childOrganizationData[i];
                var pData=organizationData[cData.parentCode];
                var children=pData.children||{};
                children[cData.code]=cData;
                pData.children=children;
            }
            // 默认值;
            changeValue('',parentOrganizationData,organizationData);
        }
    });
    $("select[name='parentOrganization']").bind("change",function (event) {
        $("#parentOrganizaitonName").html('').append(event.currentTarget.selectedOptions[0].text);
        $("#parentOrganizaitonCode").html('').append(event.currentTarget.selectedOptions[0].value);
        const value=this.value;
        changeValue(value,parentOrganizationData,organizationData);

    });
    $("select[name='organization']").bind("change",function (event) {
        $("#organizaitonName").html('').append(event.currentTarget.selectedOptions[0].text);
        $("#organizaitonCode").html('').append(event.currentTarget.selectedOptions[0].value);
        const value=this.value;
        changeValue(value,parentOrganizationData,organizationData);
    });
}